

$(document).ready(function (){
  goodshow();
});
function goodshow(){
  $("#product-table").empty();
    var id = document.getElementById("userid").innerText;
    // console.log(id);
    var data = {
        seller: id
    }

$.ajax({
    url: "http://8.130.103.251:8080/good/getGoodBySellerId",    //请求的url地址
    dataType: "json",   //返回格式为json
    ContentType: 'application/json;charset=utf-8', //传输格式
    async: true, //请求是否异步，默认为异步，这也是ajax重要特性
    data: data,
    type: "GET",   //请求方式
  
   success: function(req) {
    //  console.log(req);
        var total = req.length;
            /*容器2参数*/
        var obj_2={
        obj_box:'#product-table',//翻页容器
        total_item: total,//条目总数
        per_num:5,//每页条目数
        current_page:1//当前页
      };
      /*调用分页方法,初始化数据*/
      page_ctrl(obj_2);

        function page_ctrl(data_obj) {
        var obj_box=(data_obj.obj_box!== undefined)?data_obj.obj_box:function () {
          return;
        };//翻页容器dom对象,必要参数
      
        var total_item=(data_obj.total_item!== undefined)?parseInt(data_obj.total_item):0;//数据条目总数,默认为0,组件将不加载
      
        var per_num=(data_obj.per_num!== undefined)?parseInt(data_obj.per_num):5;//每页显示条数,默认为5条
      
        var current_page=(data_obj.current_page!== undefined)?parseInt(data_obj.current_page):1;//当前页,默认为1
      
        var total_page=Math.ceil(total_item/per_num);//计算总页数,不足2页,不加载组件
      
        // if(total_page<2){
        //   return;
        // }
      
        //在指定容器内加载分页数据
      
        // $(obj_box).append('<div class="page_content"></div>');
        $(obj_box).append('<div class="page_content"></div>');
      
        //在指定容器内加载分页插件
      
        $(obj_box).append('<div class="page_ctrl"></div>');

      
        function page_even() {
          /*加载数据*/
            function change_content() {
              
            var p = total / 5 + 1;
          if(current_page <= p){
            var n =  current_page * 5 - 5
            var page_content=$('<tbody class ="gooddata" id="gd"></tbody>');
            // var index = 0;

            while((n < total) && (n < current_page * 5))
            {
              // var trb = "<tr class='pro-list' value='" + index + "'>";
              // var tre = "</tr>"
                var tr = $("<tr class='pro-list'></tr>");
                var user = req[n];
                var td1 = "<td class='pro-lino'>" + user.goodid + "</td>";
                var td2 = "<td class='pro-lipic'>" + "<img src='" + user.goodImg1 +  "'alt='商品图片' class='con-pic'></img>" + "</td>";
                var td3 = "<td class='pro-liname'>" + user.goodName + "</td>";
                var td4 = "<td class='pro-linum'>" + user.goodNum + "</td>";
                var td5 = "<td class='pro-limoney'>" + user.dayPrice + "</td>";

                var btn = "<td class='pro-libtn'>" +
                    "<button class='btn-look'>查看详情</button>"+
                    "<button class='btn-down'>下架商品</button>"+
                    "</td>";

                // var tr = trb +  td1 + td2 + td3 + td4 + td5 + btn + tre;
                tr.append(td1);
                tr.append(td2);
                tr.append(td3);
                tr.append(td4);
                tr.append(td5);
                tr.append(btn);
                page_content = page_content.append(tr);
                // console.log(n);

                n = n + 1;
                // ++index;
            }
            $(obj_box).children('.page_content').html(page_content);
          }
            
          }
      
          change_content();
          var inp_val=(current_page==total_page)?1:current_page+1;//跳转页数,input默认显示值
          var append_html='<button class="prev_page">上一页</button>';
          for(var i=0;i<total_page-1;i++){
            if(total_page>8&&current_page>6&&i<current_page-3){
              if(i<2){
                append_html+='<button class="page_num">'+(i+1)+'</button>';
              }
              else if(i==2){
                append_html+='<span class="page_dot">•••</span>';
              }
            }
      
            else if(total_page>8&&current_page<total_page-3&&i>current_page+1){
              if(current_page>6&&i==current_page+2){
                append_html+='<span class="page_dot">•••</span>';
              }else if(current_page<7){
                if(i<8){
                  append_html+='<button class="page_num">'+(i+1)+'</button>';
                }else if(i==8){
                  append_html+='<span class="page_dot">•••</span>';
                }
              }
              //append_html+='<span class="page_dot">•••</span>';
            }
      
            else{
              if(i==current_page-1){
                append_html+='<button class="page_num current_page">'+(i+1)+'</button>';
              }
      
              else{
                append_html+='<button class="page_num">'+(i+1)+'</button>';
              }
            }
          }
      
          if(current_page==total_page){
            append_html+='<button class="page_num current_page">'+(i+1)+'</button>';
          }else{
            append_html+='<button class="page_num">'+(i+1)+'</button>';
          }
      
          append_html+='<button class="next_page">下一页</button><span class="page_total">共 '+total_page+' 页, 到第</span><input class="input_page_num" type="text" value="'+inp_val+'"><span class="page_text">页</span><button class="to_page_num">确定</button>';
      
          $(obj_box).children('.page_ctrl').append(append_html);
          if(current_page==1){
            $(obj_box+' .page_ctrl .prev_page').attr('disabled','disabled').addClass('btn_dis');
          }else{
            $(obj_box+' .page_ctrl .prev_page').removeAttr('disabled').removeClass('btn_dis');
          }
      
          if(current_page==total_page){
            $(obj_box+' .page_ctrl .next_page').attr('disabled','disabled').addClass('btn_dis');
          }else{
            $(obj_box+' .page_ctrl .next_page').removeAttr('disabled').removeClass('btn_dis');
          }
        }
      
        page_even();
        $(obj_box+' .page_ctrl').on('click','button',function () {
          var that=$(this);
          if(that.hasClass('prev_page')){
            if(current_page!=1){
              current_page--;
              that.parent('.page_ctrl').html('');
              page_even();
            }
          }
      
          else if(that.hasClass('next_page')){
            if(current_page!=total_page){
              current_page++;
              that.parent('.page_ctrl').html('');
              page_even();
            }
          }
      
          else if(that.hasClass('page_num')&&!that.hasClass('current_page')){
            current_page=parseInt(that.html());
            that.parent('.page_ctrl').html('');
            page_even();
          }
      
          else if(that.hasClass('to_page_num')){
            current_page=parseInt(that.siblings('.input_page_num').val());
            that.parent('.page_ctrl').html('');
            page_even();
          }
        });
      
      }
      
    },

});
}